﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Module.Interfaces;
using Core.Module.Interfaces.Manager;
using Core.Module.Interfaces.Model;
using umbraco.cms.businesslogic.media;

namespace Module.MultiSiteFields.Managers
{
    public class MediaNodeManager:INodeManager<IBackOffice>
    {

        public IModuleNode Get(int nodeid)
        {
            Media m = new Media(nodeid);
            if (m==null || m.Id < 0) return null;
            return ToModuleNode(m); 
        }

        public IModuleNode ToModuleNode(Media node)
        {
            IModuleNode result = ObjectManager.Resolve<IModuleNode>();
            result.Data = node;
            if (node.ContentType != null)
            {
                result.Alias = node.ContentType.Alias;
            }
            result.Icon = node.ContentTypeIcon;
            result.Name = node.Text;
            result.Id = node.Id.ToString();
            result.Title = (GetPropertyValue(result, "title") as string)??result.Name;
            result.Url = null;
            result.Path = node.Path ?? "";
            result.Parents = result.Path.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            result.SiteId = result.Parents != null && result.Parents.Length > 0 ? result.Parents[1] : result.Id;
            return result;
        }

        public IModuleNode GetChild(int parentId, string nodeTypeName)
        {
            throw new NotImplementedException();
        }

        public IModuleNode GetChild(IModuleNode parent, string nodeTypeName)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<IModuleNode> GetChildren(int parentId)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<IModuleNode> GetChildren(int parentId, string nodeTypeName)
        {
            throw new NotImplementedException();
        }

        public object GetPropertyValue(IModuleNode node, string propertyName)
        {
            Media fNode = node.Data as Media;

            umbraco.cms.businesslogic.property.Property prop = fNode.getProperty(propertyName);
            if (prop == null) return null;
            return prop.Value;
        }

        public IModuleNode GetPropertyAsNode(IModuleNode node, string propertyName)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<KeyValuePair<string, string>> GetProperties(IModuleNode node)
        {
            throw new NotImplementedException();
        }
    }
}
